package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    /*
    *
    *             入库:1.图片信息
    *                 2.公寓配套
    *                 3.公寓标签
    *                 4.公寓杂费
    *           多对多关系中,中间表的修改操作一般都是删除操作,然后再添加  根据主表id操作
    *
    * */
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;



    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

        //检查时删除还是修改
        if (apartmentSubmitVo.getId() !=null){
          long id =  apartmentSubmitVo.getId();
          //1.删除图片
            LambdaQueryWrapper<GraphInfo> query = new LambdaQueryWrapper<>();
            query.eq(GraphInfo::getItemId,id)
                            .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(query);

            //2.删除公寓配套
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //根据ApartmentFacility这张表来获取Apatrment的id,与传过来的id进行比较是否相等,然后进行删除
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);
            //3.删除公寓标签
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(labelLambdaQueryWrapper);

            //4.删除公寓杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
        }

        //代码补充  ,前端只传递了省市县的id号,应该查询数据的名称后入库
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());


        //先将公寓入库,如果是主键自增,则自动回显数据   回显id
        this.saveOrUpdate(apartmentSubmitVo);
        long apartmentId = apartmentSubmitVo.getId();

        //新增图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if ( !CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            //将用户的数据vo转化为可以入库的info对象
            graphVoList.forEach(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentId);
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            });
            //批量入库
            graphInfoService.saveBatch(graphInfoList);
        }

        //新增配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> facilityList = new ArrayList<>(facilityInfoIds.size());
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            facilityInfoIds.forEach(facilityInfoId ->{
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .facilityId(facilityInfoId)
                        .apartmentId(apartmentId)
                .build();
                facilityList.add(apartmentFacility);
            });
            apartmentFacilityService.saveBatch(facilityList);
        }


        //新增标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> labelList = new ArrayList<>(labelIds.size());
        if (!CollectionUtils.isEmpty(labelIds)){
            labelIds.forEach(labelId ->{
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .labelId(labelId)
                        .apartmentId(apartmentId)
                        .build();
                labelList.add(apartmentLabel);
            });
            apartmentLabelService.saveBatch(labelList);
        }

        //新增杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> feeValueList = new ArrayList<>(feeValueIds.size());
        if (!CollectionUtils.isEmpty(feeValueIds)){
            feeValueIds.forEach(feeValueId ->{
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .feeValueId(feeValueId)
                        .apartmentId(apartmentId)
                        .build();
                feeValueList.add(apartmentFeeValue);
            });
            apartmentFeeValueService.saveBatch(feeValueList);
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
         page = apartmentInfoMapper.pageItem(page,queryVo);
         return page;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        return apartmentInfoMapper.getApartmentDetailById(id);
    }



    //删除操作
    @Override
    public void removeApartmentById(Long id) {

        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfo = new LambdaQueryWrapper<>();
        graphInfo.eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,1);
        graphInfoService.remove(graphInfo);

        //根据传过来的公寓id,在ApartmentFacility这张表中的公寓id比较,相等则执行下一步,删除
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //删除标签 同上
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //删除杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //删除公寓自己
        this.removeById(id);

    }



}




